package pnu.ssm.sbchild;

import pnu.ssm.http.HttpConnector;
import pnu.ssm.key.Key;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PairingActivity extends Activity implements Button.OnClickListener{

	public static final String TAG = "PairingActivity --- ";

	private EditText editTextParentNumber;
	private EditText editTextChildName;
	private Button btnPairing;
	private Button btnCancelParing;

	private HttpConnector httpConnector;
	private ProgressDialog progressDialog;

	private SharedPreferences pref;
	private SharedPreferences.Editor editor;

	/*private static final int NEXT_INTENT = 1;
	private Handler mHandelr = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case NEXT_INTENT :

					break;
			}
			super.handleMessage(msg);
		}
	};*/

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_pairing);

		buildUI();
	}

	public void buildUI(){
		pref = getSharedPreferences(Key.prefKey, MODE_PRIVATE);
		editor = pref.edit();

		progressDialog = new ProgressDialog(this);
		progressDialog.setTitle("Requesting to server...");
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);

		editTextParentNumber = (EditText) findViewById(R.id.et_parent_number);
		editTextChildName = (EditText) findViewById(R.id.et_child_name);

		btnPairing = (Button) findViewById(R.id.btn_send_pairing);
		btnPairing.setOnClickListener(this);

		btnCancelParing = (Button) findViewById(R.id.btn_cancel_pairing);
		btnCancelParing.setOnClickListener(this);

		httpConnector = HttpConnector.getInstance();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();

		switch (id) {
		case R.id.btn_send_pairing:
			Log.d(TAG, "pressed send button");
			String parent_phone_number = editTextParentNumber.getText().toString();
			String child_name = editTextChildName.getText().toString();
			pairWithParent(parent_phone_number, child_name);
			break;

		case R.id.btn_cancel_pairing:
			Log.d(TAG, "pressed cancel button");
			finish();
			break;

		default:
			Log.e(TAG, "onClick Error : " + id + " (id)");
			break;
		}
	}

	private void pairWithParent(String phone_number, String child_name) {
		new PairingAsyncTask().execute(phone_number, child_name);
	}

	private class PairingAsyncTask extends AsyncTask<String, Void, String> {
		private String phone_num;
		private String child_name;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			progressDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			//부모의 Phone number를 서버에 넘긴다.
			Log.i(TAG, "doInBackground");
			phone_num = params[0];
			child_name = params[1];

			String child_num = httpConnector.sendParentPhone(phone_num, child_name);
			
			if(child_num.contains("fail")) {
				child_num = "fail";
			} else {
				String result = httpConnector.sendAppList(child_num, PairingActivity.this);
				Log.d(TAG, "[applist send] "+result);
			}
			
			return child_num;
		}

		@Override
		protected void onPostExecute(String child_num) {
			super.onPostExecute(child_num);
			Log.d(TAG, "onPostExecute() - result : " + child_num.toString());

			if(child_num.equals("fail")){
				Toast.makeText(PairingActivity.this, "Incorrect phone number!", Toast.LENGTH_LONG).show();
			}else{
				editor.putString(Key.child_num, child_num);
				editor.putBoolean(Key.hasParent, true);
				editor.putString(Key.parent_phone_num, phone_num);
				editor.commit();

				Intent intent = new Intent(PairingActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
			
			progressDialog.dismiss();
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
			
			Toast.makeText(PairingActivity.this, "요청이 취소되었습니다.", Toast.LENGTH_LONG).show();
		}
	}
}
